import Gobj from "./Gobj";
import GMap from "./GMap";
import GEnemy from "./GEnemy";
import GManager from "./GManager";
import MaoRandom from "../MaoRandom";
import GameWx from "../GameWx";

export default class GUniverse {

    //static uids = [];
    random: MaoRandom = new MaoRandom();

    constructor(uid) {
        this.uid = uid;
        // if(GUniverse.uids.length>=2){
        //     throw new Error("GUniverse.uids.length >=2 : " + GUniverse.uids.length);
        // }
        // GUniverse.uids.push[uid];
    }



    static C = 10000;
    uid = 0;
    sf: number = 0;
    lf: number = 0;
    fms: number = 0;
    keyf: boolean = false;
    actionMap = {};
    //map = new GMap();

    //e:GEnemy = new GEnemy();
    gm: GManager

    init_() {
        this.sf = 0;
        this.lf = 0;
        this.fms = 0;
        this.sf = 0;
        this.actionMap = {};
        this.gm = new GManager(this.uid);
        // this.map = new GMap();
        // this.map.addObj(this.gm);

        this.random.seed = GameWx.I.seed;
        //console.log("===========测试随机数=================" , GameWx.I.seed );        
        // for (let i = 0; i < 10; i++) {
        //     console.log(this.random.seededRandom() );
        // }
    }

    timeline(sf: number, lf: number, fms: number, keyf: boolean) {
        if(this.gm.gameover){return;}

        this.sf = sf;
        this.lf = lf;
        this.fms = fms;
        this.keyf = keyf;
        var key = "ac_" + sf;

        if (this.keyf && this.actionMap[key]) {//执行action
            this.gm.timeline(sf, lf, fms, keyf, this, this.actionMap[key]);
        } else {
            this.gm.timeline(sf, lf, fms, keyf, this, null);
        }

        // var arr = this.map.objArr.concat();
        // if(this.keyf && this.actionMap[key]){//执行action
        //     for (let i = 0; i < arr.length; i++) {
        //         let obj = arr[i] as Gobj;
        //         obj.timeline(sf,lf,fms,keyf,this,this.actionMap[key]);
        //     }
        // }else{
        //     for (let i = 0; i < arr.length; i++) {
        //         let obj = arr[i] as Gobj;
        //         obj.timeline(sf,lf,fms,keyf,this,null);
        //     }
        // }

        // if(this.uid==2){
        //     Laya.stage.addChild(this.e);
        // }
        // this.e.efe(lf,"e_"+this.uid);        
    }

    /**同步平行宇宙数据 */
    syncTime(otherUnivers: GUniverse) {
        this.random.seed = otherUnivers.random.seed;
        this.gm.syncTime( otherUnivers.gm );

        // var omap = otherUnivers.map.objMap;
        // var mmap = this.map.objMap;

        // //同步两个宇宙的对象数据
        // for (const key in omap) {
        //     let oobj = omap[key] as Gobj;
        //     let mobj = mmap[key] as Gobj;
        //     if (mobj) {//如果存在 就同步数据
        //         mobj.syncTime(oobj);
        //     } else {//如果不存在 就克隆一个数据
        //         mobj = oobj.copy();
        //         mobj.uid = this.uid;
        //         this.map.addObj(mobj);
        //     }
        // }

        // //同步删除不存在的对象数据
        // var temp = [];
        // for (const key in mmap) {
        //     if (!omap[key]) {
        //         temp.push(key);
        //     }
        // }
        // for (let i = 0; i < temp.length; i++) {
        //     const key = temp[i];
        //     this.map.removeobj(key);
        // }

        // this.map.sortArr();

    }
}